<?PHP
	$db_host = "localhost";
	$db_user = "root";
	$db_password = "";
	$db_database_name = "BSST";
	$mysqli = new MySQLi($db_host,$db_user,$db_password,$db_database_name);
	
	if($mysqli->connect_error){
		die('Connect Error ('.$mysqli->connect_errno.') '.$mysqli->connect_error);
	}
	
	isset($_REQUEST['nodes']) ? $nodes = $_REQUEST['nodes'] : $nodes=200;
	$timestamp = $_REQUEST['start'];
	
	$query = 'SELECT * FROM ping /*WHERE (id%2=0)*/ ORDER BY timestamp DESC LIMIT '.$nodes;
	if(isset($timestamp))
		$query = 'SELECT * FROM ping WHERE timestamp > "'.$timestamp.'" ORDER BY timestamp LIMIT '.$nodes;
		
	if($result = $mysqli->query($query)){
		while($row = $result->fetch_object()){
			if($row->min > 999) $row->min=999;
			else $row->min = round($row->min);
			
			if($row->avg > 999) $row->avg=999;
			else $row->avg = round($row->avg);
			
			if($row->max > 999) $row->max=999;
			else $row->max = round($row->max);
			$data[] = $row;
		}
		sort($data);
	}
	else echo'No results using query: <i><b>'.$query.'</b></i>';

/*	$datasize = sizeof($data);
	for($i=2; $i<$datasize; $i=$i+5){
		$avg = round(($data[$i-2]->avg + $data[$i-1]->avg + $data[$i]->avg + $data[$i+1]->avg + $data[$i+2]->avg)/5);
		$data[$i]->avg = $avg;
		unset($data[$i-2],$data[$i-1],$data[$i+1],$data[$i+2]);
	}
	array_pop($data);
*/
?>
<HTML>
<head>
	<title>Jesuso server.</title>
	<script type="text/javascript" src="https://www.google.com/jsapi"></script>
	<script type="text/javascript">
		function dump(arr,level) {
			var dumped_text = "";
			if(!level) level = 0;
			
			//The padding given at the beginning of the line.
			var level_padding = "";
			for(var j=0;j<level+1;j++) level_padding += "    ";
			
			if(typeof(arr) == 'object') { //Array/Hashes/Objects 
				for(var item in arr) {
					var value = arr[item];
					
					if(typeof(value) == 'object') { //If it is an array,
						dumped_text += level_padding + "'" + item + "' ...\n";
						dumped_text += dump(value,level+1);
					} else {
						dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
					}
				}
			} else { //Stings/Chars/Numbers etc.
				dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
			}
			return dumped_text;
		}


		google.load("visualization", "1", {packages:["corechart"]});
		google.setOnLoadCallback(drawChart);
			function drawChart() {
				var JSONObject = {
					cols:[
						{id: 'fecha', label: 'Fecha', type: 'string'},
						{id: 'min', label: 'Minimo (ms)', type: 'number'},
						{id: 'max', label: 'Maximo (ms)', type: 'number'},
						{id: 'med', label: 'Media (ms)', type: 'number'},
						{id: 'loss', label: '% Perdidos (*10)', type: 'number'},
					],
					rows:[
<?PHP
	foreach($data as $key => $line){
		echo "\t\t\t\t\t\t{c:[{v: '$line->timestamp'}, {v: $line->min}, {v: $line->max}, {v: $line->avg}, {v: ".($line->loss*10)."}]},\t//$line->id ($key)\n";
	}
?>
					]
				};
				
				var data = new google.visualization.DataTable(JSONObject, 0.5);
				
				var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
				chart.draw(data, {width: 1200, height: 600, title: 'Latency Log'});
				
				google.visualization.events.addListener(chart, 'select', selectHandler);
				
				function selectHandler(){
					var selection = chart.getSelection();
				}
      }
    </script>
</head>
<body>
	<div id="chart_div" align="center"></div>
	<form>
		Inicio:
		<input type="datetime" value="<?PHP echo $timestamp; ?>" name="start">
		Nodos:
		<input type="number" max="1000" min="20" value="<?PHP echo $nodes; ?>" name="nodes">
		<input type="submit" value="Mostrar">
	</form>
	<br>
	<a href="ping24.log">Registro 24 Octubre</a><br>
	<a href="ping17-23.log">Registro 17-23 Octubre</a>
</body>
</HTML>